/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mbean.admin.attendance;

import java.io.IOException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import mbean.AbstractMB;
import model.entity.Attendance;
import model.entity.Depot;
import model.entity.Driver;
import model.entity.Vehicle;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.omnifaces.util.Faces;
import org.omnifaces.util.Messages;
import service.InterfaceServiceLocal;
import utils.HttpmethodUtil;
import utils.URL;
import utils.URLBuilder;

/**
 *
 * @author dingnangen
 */
@Named(value = "attendanceCreateMB")
@SessionScoped
public class AttendanceCreateMB extends AbstractMB implements Serializable {

    @EJB(beanName = "AttendanceManager")
    private InterfaceServiceLocal<Attendance> attendanceManager;
    @EJB(beanName = "DepotManager")
    private InterfaceServiceLocal<Depot> depotManager;
    @EJB(beanName = "VehicleManager")
    private InterfaceServiceLocal<Vehicle> vehicleManager;
    @EJB(beanName = "DriverManager")
    private InterfaceServiceLocal<Driver> driverManager;
//    private String currentdepot;
//    private String currentDriver;
//    private String currentVehicle;
    private Attendance currentAttendance;
    List<Depot> depotlist;
    List<Driver> driverlist;
    List<Vehicle> vehiclelist;

    /**
     * 初始化
     */
    public void init() {
        // currentdepot = "";
//        currentDriver = "";
//        currentVehicle = "";
        this.currentAttendance = new Attendance();
        this.currentAttendance.setAttendancedate(new Date());
        depotlist = depotManager.findAll();

        if (this.getDepotlist() != null && !this.getDepotlist().isEmpty()) {
            this.currentAttendance.setIddepot(this.getDepotlist().get(0));
            refreshDriverlist();
            refreshVehiclelist();

        }

    }

    public void precreateAttendance() {
        if (Faces.isPostback()) {
            System.out.println("isPostback");
            return;
        }
        init();

    }

    public void create() {
//        currentAttendance.setIddriver(driverManager.find(Integer.parseInt(currentDriver)));
//        currentAttendance.setIdvehicle(vehicleManager.find(Integer.parseInt(currentVehicle)));
//        currentAttendance.setIddepot(depotManager.find(Integer.parseInt(currentdepot)));
        if (currentAttendance.getIddriver() != null && currentAttendance.getIdvehicle() != null) {
            attendanceManager.create(currentAttendance);
            System.out.println("create finish");
            Messages.create(Faces.getResourceBundle("bundle").getString("saveSuccess")).add();
            HttpmethodUtil.updateEntity(currentAttendance.getIddriver().getIduser().getLoginname(), "plate", currentAttendance.getIdvehicle().getPlate());

            init();
        } else {
            Messages.create("车辆或驾驶员不能为空!").add();
        }

    }

    public void ondepotchange() {
        refreshDriverlist();
        refreshVehiclelist();

    }

    public void onattendancedatechange() {
        refreshDriverlist();
        refreshVehiclelist();

    }

    public void ondriverchange() {
        refreshVehiclelist();
        // currentVehicle = driverManager.find(Integer.parseInt(currentDriver)).getIdvehicle().getPlate();
    }

    public void refreshVehiclelist() {
        if (this.getDepotlist() == null || this.getDepotlist().isEmpty()) {
            return;
        }
        List p = new ArrayList();
//        Calendar calendar = new GregorianCalendar(2016, 8, 26, 0, 0, 0);

        p.add(this.currentAttendance.getAttendancedate());
        p.add(this.currentAttendance.getIddepot());

        vehiclelist = vehicleManager.findAll("select v from Vehicle v where v not in (select a.idvehicle from Attendance a where a.attendancedate =?1) and v.iddepot=?2", p);
        if (vehiclelist != null && !vehiclelist.isEmpty() && driverlist != null && !driverlist.isEmpty()) {
            this.currentAttendance.setIdvehicle(this.currentAttendance.getIddriver().getIdvehicle());
        }
    }

    public void refreshDriverlist() {
        if (this.getDepotlist() == null || this.getDepotlist().isEmpty()) {
            return;
        }
        List p = new ArrayList();
//        Calendar calendar = new GregorianCalendar(2016, 8, 26, 0, 0, 0);

        p.add(this.currentAttendance.getAttendancedate());
        p.add(this.currentAttendance.getIddepot());

        driverlist = driverManager.findAll("select d from Driver d where d not in (select a.iddriver from Attendance a where a.attendancedate =?1) and d.iddepot=?2", p);
        if (driverlist != null && !driverlist.isEmpty()) {
            this.currentAttendance.setIddriver(driverlist.get(0));
        }
    }

    /**
     * Creates a new instance of CustomerCreateMB
     */
    public AttendanceCreateMB() {
    }

    public Attendance getCurrentAttendance() {
        return currentAttendance;
    }

    public void setCurrentAttendance(Attendance currentAttendance) {
        this.currentAttendance = currentAttendance;
    }

    public List<Depot> getDepotlist() {
        return depotlist;
    }

    public void setDepotlist(List<Depot> depotlist) {
        this.depotlist = depotlist;
    }

    public List<Driver> getDriverlist() {
        return driverlist;
    }

    public void setDriverlist(List<Driver> driverlist) {
        this.driverlist = driverlist;
    }

    public List<Vehicle> getVehiclelist() {
        return vehiclelist;
    }

    public void setVehiclelist(List<Vehicle> vehiclelist) {
        this.vehiclelist = vehiclelist;
    }

}
